/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.DeviceService;
import com.lmx.persistence.service.DicttypeService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Dicttype;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(Dicttype.class)
@Component
public class DicttypeRepository extends BaseRepository<Dicttype> {

	@Autowired
	private DicttypeService srv;

	@Autowired
	private DeviceService deviceService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(Dicttype.class);
	}

	// @Override
	// @JsonApiFindAll
	// public Iterable<Dicttype> findAll() {
	// Long tenantId = ActionContextHolder.getContext().getTenantId();
	// if (tenantId<=ICommonConstants.ADMIN_TENANT_ID_THRESHOLD) {
	// return super.findAll();
	// }else{
	// return srv.findDicTypeOfTenant(tenantId);
	// }
	// }

	@Override
	@JsonApiSave
	public Dicttype save(Dicttype model) {
		if (model.getOperateType() != null
				&& model.getOperateType().contains("deviceType")) {
			if (model.getId() == null) {
				if (srv.findByName(model) != null) {
					model.setErrcode(4);// name重复
					throw new BusinessException(null,
							CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
				if (srv.findByCode(model.getTypecode()) != null) {
					model.setErrcode(12);// code重复
					throw new BusinessException(null,
							CommonConstants.ERROR_CODE.TYPECODE_ERROR.label(),
							null);
				}
				srv.createDeviceType(model); // 新增设备类型
			} else {
				if (model.getDelStatus() == 1) {// 删除
					if (deviceService.getDeviceByDictType(model).size() > 0) {
						model.setErrcode(8);// no delete
						throw new BusinessException(null,
								CommonConstants.ERROR_CODE.NO_DELETE.label(),
								null);
					} else {
						srv.deleteDeviceType(model);// 删除设备类型
					}
				} else {
					if (srv.findByNameFilterId(model) != null) {
						model.setErrcode(4);// 名字重复
						throw new BusinessException(null,
								CommonConstants.ERROR_CODE.NAME_ERROR.label(),
								null);
					}
					if (srv.findByCodeFilterId(model) != null) {
						model.setErrcode(12);// code重复
						throw new BusinessException(null,
								CommonConstants.ERROR_CODE.TYPECODE_ERROR
										.label(), null);
					}
					super.save(model);// 修改
				}
			}
		} else {
			// 通用逻辑
			if (CommonConstants.DEL_STATUS_YES_1.equals(model.getDelStatus())) {
				this.srv.checkDelete(model);
			} else {
				this.srv.checkRepeat(model);
			}
			super.save(model);
		}
		return model;
	}
}
